{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world! 👋 How can I assist you today? 😊\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "if not os.environ.get(\"DEEPSEEK_API_KEY\"):\n",
    "    os.environ[\"DEEPSEEK_API_KEY\"] = 'sk-72c548e2ff234f66af740f685b3895fb'\n",
    "\n",
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "model = init_chat_model(\"deepseek-chat\", model_provider=\"deepseek\")\n",
    "\n",
    "response = model.invoke(\"Hello, world!\")\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\15953\\.conda\\envs\\langchain\\lib\\site-packages\\langchain_experimental\\sql\\base.py:77: UserWarning: Directly instantiating an SQLDatabaseChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025年丰田汽车总销量是5700辆。\n"
     ]
    }
   ],
   "source": [
    "from langchain_community.utilities import SQLDatabase\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_experimental.sql import SQLDatabaseChain\n",
    "\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek API端点\n",
    "    model=\"deepseek-chat\",                  # DeepSeek模型标识\n",
    "    openai_api_key=\"sk-72c548e2ff234f66af740f685b3895fb\",     # 替换为DeepSeek密钥\n",
    "    max_tokens=10000,\n",
    "    temperature=0.1\n",
    ")\n",
    "\n",
    "new_db = SQLDatabase.from_uri(\"sqlite:///car_sales.db\")\n",
    "new_db_chain = SQLDatabaseChain(llm=llm, database=new_db)\n",
    "question = \"2025年丰田汽车总销量是多少？\"\n",
    "result = new_db_chain.run(question)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\15953\\.conda\\envs\\langchain\\lib\\site-packages\\langchain_experimental\\sql\\base.py:77: UserWarning: Directly instantiating an SQLDatabaseChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SQLQuery: SELECT \"company_name\" FROM \"companies\" WHERE \"parent_company_id\" = 5 LIMIT 5\n"
     ]
    }
   ],
   "source": [
    "db = SQLDatabase.from_uri(\"sqlite:///company_database.db\")\n",
    "db_chain = SQLDatabaseChain(llm=llm, database=db)\n",
    "question = \"东风集团有哪几家子公司，分别说出公司名称\"\n",
    "result = db_chain.run(question)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SQLQuery: SELECT \"company_name\" FROM \"companies\" WHERE \"parent_company_id\" = 1 LIMIT 5\n"
     ]
    }
   ],
   "source": [
    "question2 = \"一汽集团有哪几家子公司？\"\n",
    "result2 = db_chain.run(question2)\n",
    "print(result2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Answer: {17: '上汽大众汽车有限公司', 18: '上汽通用汽车有限公司', 19: '上汽通用五菱汽车股份有限公司', 20: '南京依维柯汽车有限公司', 21: '上汽红岩汽车有限公司'}\n"
     ]
    }
   ],
   "source": [
    "question3 = \"上汽集团有哪几家子公司？以{id:公司名称}的形式返回结果\"\n",
    "result3 = db_chain.run(question3)\n",
    "print(result3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
